package com.xintonghua.bussiness.ui.user.manage;

import android.os.Bundle;

import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.MyUtils;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.EnumType;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.GoodAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.GoodsManageBean;
import com.xintonghua.bussiness.bean.GoodsManageResponse;
import com.xintonghua.bussiness.util.MyState;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：wanxianyi
 * 时间：2018/3/30.
 * 包名：com.xintonghua.bussiness.ui.user.manage
 */

public class ProductListActivity extends BaseActivity {
    @BindView(R.id.lv_search_product)
    XRecyclerView lvSearchProduct;
    String keyword = "";
    private int page = 1;
    private List<GoodsManageBean> manageBeans = new ArrayList<>();
    GoodsManageResponse response;
    GoodAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_list);
        ButterKnife.bind(this);
        initUI();

    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        setSimpleActionBar("搜索结果");
        RefreshUtils.initList(this, lvSearchProduct, MyUtils.dip2px(this, 2), R.color.grey_bg);
       // keyword = getIntent().getStringExtra(BuildConfig.FLAVOR);
        adapter = new GoodAdapter(this, manageBeans);
        adapter.setCheck(true);
        lvSearchProduct.setAdapter(adapter);
        lvSearchProduct.setLoadingListener(this);
        find();
    }

    private void find() {
        httpCent.stockProducts(page, keyword, EnumType.ALL,
                this, MyState.requestOne);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                if (page == 1) {
                    manageBeans.clear();
                }
                response = JSONObject.parseObject((String) data, GoodsManageResponse.class);
                manageBeans.addAll(response.getList());
                adapter.notifyDataSetChanged();
                RefreshUtils.onRefresh(adapter, lvSearchProduct);
                break;
        }

    }

    @Override
    public void onRefresh() {
        page = 1;
        find();
    }

    @Override
    public void onLoadMore() {
        if (RefreshUtils.hasNext(response, adapter, lvSearchProduct)) {
            page++;
            find();
        }
    }
}
